//
//  CZDropDownMenu.m
//  CZOC微博01
//
//  Created by 1 on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "CZDropDownMenu.h"
#import "UIView+CZExtension.h"
@interface CZDropDownMenu()
@property (nonatomic,strong) UIImageView *contentView;

@end
@implementation CZDropDownMenu
- (UIImageView *)contentView
{
    if(!_contentView)
     {
         _contentView=[[UIImageView alloc]init];
        _contentView.image=[UIImage imageNamed:@"popover_background"];

        _contentView.width=200;
        _contentView.height=300;
         _contentView.userInteractionEnabled=YES;
      } 
   return _contentView;
}
- (void)setContent:(UIView *)content
{
    self.contentView.userInteractionEnabled=YES;
    _content=content;
    content.x=10;
    content.y=20;
    
    self.contentView.height=CGRectGetMaxY(content.frame)+10;
    content.width=self.contentView.width-2*content.x;
    
    [self.contentView  addSubview:content];
    
}
 - (id)initWithFrame:(CGRect)frame
{
    if(self=[super initWithFrame:frame])
    {
        self.backgroundColor=[UIColor clearColor];

        
        [self addSubview:self.contentView];
    }
    return self;
}

+ (instancetype)menu
{
    return [[self alloc]init];
    
}
- (void)show:(UIView *)from
{
    
    
   UIWindow *window= [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    //必须能设置它的farme
    self.frame=window.bounds;
//    CGRect newRect=[from convertRect:from.bounds fromView:window];
    
    
    CGRect newRect=[from convertRect:from.bounds toView:window];
    
    
    self.contentView.centerX=CGRectGetMidX(newRect);
//    self.contentView.x=newRect.origin.x;
    self.contentView.y=CGRectGetMaxY(newRect);

    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}


- (void)dismiss
{
    [self removeFromSuperview];
    if([self.delegate respondsToSelector:@selector(dorpDownMenuDismiss:)])
    {
        [self.delegate dorpDownMenuDismiss:self];
    }
    
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"pullCahange" object:self userInfo:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:@"pullChange" object:nil];
    
}
//- (void)change
//{
//    NSLog(@"****KJEHGJSAF***");
//}

- (void)setController:(UIViewController *)controller
{
    _controller=controller;
    
    
//    [self.contentView addSubview:controller.view];
    self.content=controller.view;
    
 }
@end
